package com.ft.oa.service.dev.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ft.oa.domain.dev.DevAttachment;
import com.ft.oa.domain.dto.ApiResult;
import com.ft.oa.domain.dto.knowledgeBase.AttachmentDto;
import com.ft.oa.enums.DirectoryTypeEnum;
import com.ft.oa.enums.ManagerEnum;
import com.ft.oa.mapper.dev.DevAttachmentMapper;
import com.ft.oa.service.dev.DevAttachmentService;
import com.ft.oa.service.knowledgeBase.DirectoryService;
import com.ft.oa.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: yihui
 * Date: 2022/1/18 10:15 AM
 * FileName: DevTaskItemServiceImpl
 * Description: 研发任务进展
 */
@Transactional
@Service
public class DevAttachmentServiceImpl implements DevAttachmentService {

    @Resource
    DevAttachmentMapper devAttachmentMapper;

    @Resource
    DirectoryService directoryService;

    @Override
    public ApiResult add(DevAttachment devAttachment) {
        String[]  projectNumbers = devAttachmentMapper.queryProjectNumbers(devAttachment.getDevAllocateId());
        String source = "";
        if( null!=devAttachment.getDevTaskId()){
            source = DirectoryTypeEnum.DEV_TASK.getCode();
        }
        if( null!=devAttachment.getDevProblemId()){
            source = DirectoryTypeEnum.DEV_PROBLEM.getCode();
        }
        if( null!=devAttachment.getMilestoneId()){
            source = DirectoryTypeEnum.DEV_MILESTONE.getCode();
        }
        List<AttachmentDto> attachmentDtos = devAttachment.getAttachments();
        for (AttachmentDto attachmentDto : attachmentDtos) {
            DevAttachment devAtt = new DevAttachment();
            BeanUtils.copyProperties(devAttachment, devAtt);
            BeanUtils.copyProperties(attachmentDto, devAtt);
            devAtt.setCreateStaffId(SecurityUtils.getCurrentUser().getId());
            devAtt.setCreateStaffName(SecurityUtils.getCurrentUser().getUsername());
            devAtt.setCreateDateTime(LocalDateTime.now());
            String json = JSONArray.toJSONString(attachmentDto);
            devAtt.setAttachment("["+json+"]");
            devAttachmentMapper.insert(devAtt);
            //构造附件上传对象
            List<AttachmentDto> attachmentDtoList = new ArrayList<>();
            attachmentDtoList.add(attachmentDto);
            directoryService.saveProAttachment(attachmentDtoList, source,String.valueOf(devAtt.getId()),devAttachment.getFileType(),projectNumbers);
        }
        return ApiResult.simpleSuccess();
    }

    @Override
    public Page<DevAttachment> page(DevAttachment devAttachment) {
        Page<DevAttachment> pages = new Page<>(devAttachment.getPage(), devAttachment.getSize());
        QueryWrapper<DevAttachment> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(ManagerEnum.GENERAL.toString().equals(SecurityUtils.getCurrentUser().getManager()), DevAttachment::getCreateStaffId,SecurityUtils.getCurrentUser().getId())
                .eq(null != devAttachment.getDevTaskId() ,DevAttachment::getDevTaskId, devAttachment.getDevTaskId())
                .eq(null != devAttachment.getDevProblemId() ,DevAttachment::getDevProblemId, devAttachment.getDevProblemId())
                .eq(null != devAttachment.getMilestoneId() ,DevAttachment::getMilestoneId, devAttachment.getMilestoneId())
                .like(StringUtils.hasText(devAttachment.getDocName()) ,DevAttachment::getDocName, devAttachment.getDocName());
        wrapper.select().orderByDesc("create_date_time");
        Page<DevAttachment> result = devAttachmentMapper.selectPage(pages, wrapper);
        List<DevAttachment> records = result.getRecords();
        records.forEach( e ->{
            e.setAttachments(JSONObject.parseArray(e.getAttachment(), AttachmentDto.class));
            e.setAttachment("");
        });
        return result;
    }

    @Override
    public ApiResult del(DevAttachment devAttachment) {
        Long devAttachmentId = devAttachment.getId();
        devAttachmentMapper.deleteById(devAttachmentId);
        return ApiResult.simpleSuccess();
    }

}
